#include <dbus/dbus-glib.h>
#include <stdlib.h> //exit

static void dbus_callback(DBusGProxy *proxy, DBusGProxyCall *call_id, 
            void *usr_data) {
    GError *err = NULL;
    char *str = NULL;
    GMainLoop *loop = usr_data;
    dbus_g_proxy_end_call(proxy, 
                          call_id, //call id
                          &err, 
                          G_TYPE_STRING, &str, 
        //method output type/value pairs, end with G_TYPE_INVALID
                          G_TYPE_INVALID
    );
    if(err != NULL) {
        g_printerr("Error in dbus method Introspect call:%s\n", err->message);
        g_error_free(err);
    }else {
        g_print("Method Introspect call return:\n%s\n", str);
    }
    g_main_loop_quit(loop);                       
    
}


int main(int argc, char *argv[]) {
    GError *error = NULL;
    DBusGConnection *conn;
    DBusGProxy *proxy;
    GMainLoop *loop;
    //connect to session bus(DBUS_BUS_SYSTEM for system bus)
    conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
    if(conn == NULL) {
        g_printerr("Failed to open connection to bus:%s\n", error->message);
        g_error_free(error);
        exit(1);
    }
    //create a proxy object
    proxy = dbus_g_proxy_new_for_name (
        conn, //connection 
        "org.freedesktop.Notifications", //service name
        "/", //instance path
        "org.freedesktop.DBus.Introspectable" //insterface
    );
    error = NULL;
    loop = g_main_loop_new(NULL, FALSE);
    dbus_g_proxy_begin_call(proxy, 
                            "Introspect",
                            dbus_callback,
                            loop,
                            NULL, //function called to destroy user_data
                            G_TYPE_INVALID
        //method input type/value pairs, end with G_TYPE_INVALID
    );
    
    g_main_loop_run(loop);
    return 0;


}
